$(document).ready(init);

// AJAX
$.ajaxSetup( {
   url: "document.php?ajax=true",
   global: false,
   type: "POST"
 } );

function init()
{
	initDateAndTimePicker();
	
	$('#UDADOCfileList input[@type=checkbox]').click(
		function()
		{
			displaySelect();
		}
	);
	
 	$("#UDADOCselAll").click(
		function ()
		{
			$(this).parents('table').find('input[@type=checkbox]').attr('checked','checked');
			displaySelect();
		}
	);   

	$("#UDADOCreverseSel").click(
		function ()
		{
			$(this).parents('table').find('input[@type=checkbox]').each(
				function()
				{
					if ($(this).attr('checked') == true)
					$(this).removeAttr('checked');
					else
					$(this).attr('checked','checked');
				}
			);
			
			displaySelect();
		}
	);   

	$("#actionSelection").change(
		function()
		{
			// Hide dialogBox
			$('.claroMessageBox').remove();
			
			
			/*************************************
				Delete selected files
			*************************************/
			if ($(this).val() == 'delete')
			{
				if (confirmation($("#actionSelection option:first").text()))
				{
					// Add files in the list
					listeFichiersAjax = getSelectedFiles();

					if (listeFichiersAjax) {
						$.ajax({
							data: "cmd=exRm" + listeFichiersAjax,
							success: function(msg){
								if (msg != "") {
									$(".claroToolTitle:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
								}
								else {
									$("#UDADOCfileList input[@type=checkbox]:checked").each(function(){
										$(this).parents("tr").remove();
									});
								}
							},
							error: function(){
								return false;
							}
						});
					}
				}
				else
				{
					return false;
				}
			}
			
			/*************************************
			 * 	Move selected files
			 ************************************/
			if ($(this).val() == 'move') {
					// Display available folders
					$.ajax({
						data: "cmd=rqMv&ajax=true",
						success: function(msg){
							if (msg != "") {
								$(".claroToolTitle:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
							
								// Process dialog box
								// Remove submit
								$(".claroMessageBox button:last").before('<button class="claroButton" id="moveFiles">'+$(".claroMessageBox input[@type=submit]").val()+'</button>');
								$(".claroMessageBox button:last").removeAttr('onclick');
								$(".claroMessageBox button:last").click(
									function(){
										window.location.reload();
									}
								);
								$(".claroMessageBox input[@type=submit]").remove();
								$(".claroMessageBox form").submit(function(){
									return false;
								});
								
								// ExMv
								$("#moveFiles").click(
									function()
									{	
										// Add files in the list
										listeFichiersAjax = getSelectedFiles();
										if (listeFichiersAjax) 
										{
											var destination = $(".claroMessageBox select option:selected").val();
											
											// Hide dialogBox
											$('.claroMessageBox').remove();			
											
											$.ajax({
												data: "cmd=exMv&ajax=true&destination="+destination+ listeFichiersAjax,
												success: function(msg){
													if (msg != "") {
														$(".claroToolTitle:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
													}
													else {
														window.location.reload();
													}
												},
												error: function(){
													return false;
												}
											});
										}
									}
								);
							}
						},
						error: function(){
							return false;
						}
					});
			}
			
			/*************************************
			 * 	Hide selected files
			 ************************************/
			if ($(this).val() == 'hide') {
				// Add files in the list
				listeFichiersAjax = getSelectedFiles();

				if (listeFichiersAjax) {
					$.ajax({
						data: "cmd=exChVis&ajax=true&vis=i" + listeFichiersAjax,
						success: function(msg){
							if (msg != "") {
								$(".claroToolTitle:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
							}
							else {
								window.location.reload();
							}
						},
						error: function(){
							return false;
						}
					});
				}
			}
			
			/*************************************
			 * 	Show selected files
			 ************************************/
			if ($(this).val() == 'show') {
				// Add files in the list
				listeFichiersAjax = getSelectedFiles();

				if (listeFichiersAjax) {
					$.ajax({
						data: "cmd=exChVis&ajax=true&vis=v" + listeFichiersAjax,
						success: function(msg){
							if (msg != "") {
								$(".claroToolTitle:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
							}
							else {
								window.location.reload();
							}
						},
						error: function(){
							return false;
						}
					});
				}
			}
			
			$("#actionSelection option:first").attr('selected','selected');
			$("#actionSelection").hide();
		}
	);
	
				
			
	/*****************************************
	 * Deal with the visibility period form
	 ****************************************/
	UDADOCprocessVisibilityPeriod();


	/********************************
	 * Replace files on the server
	 ********************************/
	
	$("#uncompress").click(
		function()
		{
			if($("#replaceFiles").css('display') == 'none')
			$("#replaceFiles").show('fast');
			else
			{
				$("#replaceFiles").hide('fast');
				$("#replaceFiles input").attr("checked",false);
			}
		}
	);
}

function getSelectedFiles()
{
	var listeFichiersAjax = "";
	$('#UDADOCfileList').find('input[@type=checkbox]:checked').each(
		function()
		{
			listeFichiersAjax += "&file[]="+$(this).val();
		}
	);
	
	return listeFichiersAjax;
}

function displaySelect()
{
	if ($('#UDADOCfileList').find('input[@type=checkbox]:checked').size() == 0)
	$("#actionSelection").hide();
	else
	$("#actionSelection").show();
}


function UDADOCprocessVisibilityPeriod()
{
	$('#UDADOCperiods form').submit(function() {return false;});


	
	/*****************************
	 * Edit / add new period
	 ****************************/
	$(".editDates,#UDADOCnewPeriod").click(
		function(){
			
			// New period
			if ($(this).attr('id') == 'UDADOCnewPeriod') {
				$("#periodModified").val('period_new');
				
				$("#startDate").val('');
				$("#endDate").val('');
				
				$("#startHours option:first").attr('selected','selected');
				$("#endHours option:first").attr('selected','selected');
				
				$("#startMinutes option:first").attr('selected','selected');
				$("#endMinutes option:first").attr('selected','selected');
			}
			else {
				$("#periodModified").val($(this).parent().attr('id'));
				
				// Date Picker			
				$("#changeDatesBox input[@type=submit]").focus();
				var SD = $(this).prev().find(".visStartDate").text();
				var ED = $(this).prev().find(".visEndDate").text();
				
				$("#startDate").val(SD.substring(0, 8));
				$("#endDate").val(ED.substring(0, 8));
				
				var startHours = SD.substr(9,2);
				var endHours = ED.substr(9,2);
				
				var startMinutes = SD.substr(12);
				var endMinutes = ED.substr(12);
						
				$("#startHours [@value="+startHours+"]").attr('selected','selected');
				$("#endHours [@value="+endHours+"]").attr('selected','selected');
				
				$("#startMinutes [@value="+startMinutes+"]").attr('selected','selected');
				$("#endMinutes [@value="+endMinutes+"]").attr('selected','selected');
			}
			
			
			
			$("#changeDatesBox").jqm();
			$('#changeDatesBox').jqmShow();
			
			// AJAX processing
			$("#changeDatesBox button:first").click(function(){
				// Values
				var SD = $("#startDate").val();
				var ST = $("#startHours").val()+":"+$("#startMinutes").val();
				var ED = $("#endDate").val();
				var ET = $("#endHours").val()+":"+$("#endMinutes").val();
				
				if (SD == '' || ST == '' || ED == '' || ET == '') {
					alert(UDADOCget_lang('You must fill all the fields'));
					return false;
				}
				
				if (SD == ED && (ST.substring(0,2)>ET.substring(0,2) || ST.substring(3,5)>ET.substring(3,5))) {
					alert(UDADOCget_lang('Your end date must be set after the start date'));
					return false;
				}
				
				
				$.ajax({
					data: "cmd=editVisPeriod&documentId="+$('#documentId').val()+"&period="+$('#periodModified').val()+"&SD=" + SD + "&ST=" + ST + "&ED=" + ED + "&ET=" + ET,
					success: function(msg){
						if (msg != "") {
							$(".claroToolTitle:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
							$(".jqmClose").trigger('click');
						}
						else {
							window.location.reload();
						}
					},
					error: function(){
						return false;
					}
				});
			});
			
			$("#changeDatesBox button:last").click(
				function()
				{
					$(".jqmClose").trigger('click');
				}
			)
		}		
	);
	
	$(".deleteDates").click(
		function ()
		{
			if (confirm(UDADOCget_lang('This visibility period will be deleted'))) {
				$("#periodModified").val($(this).parent().attr('id'));
				
				$.ajax({
					data: "cmd=deleteVisPeriod&period=" + $('#periodModified').val(),
					success: function(msg){
						if (msg != "") {
							$(".claroToolTitle:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
						}
						else {
							window.location.reload();
						}
					},
					error: function(){
						return false;
					}
				});
			}
		}
	);

	
	
	/************************************
	 * Show / hide classes
	 ***********************************/
	$(".UDADOCshowHide").toggle(
		function()
		{
			$(this).next().show('fast');
		},
		function()
		{
			$(this).next().hide('fast');
		}
	);
	
	
	
	/***************************************
	 * Swap allUsers / limited to classes
	 **************************************/
	$("#UDADOCperiods input[@type=radio]").click(
		function(){
			if($(this).val() == 'all')
			var ajaxVal = 1;
			else
			var ajaxVal = 0;
			
			period = $(this).parents('li').attr("id");
			
			$.ajax({
				data: "cmd=editVisAcces&period="+period+"&allUsers="+ajaxVal,
				success: function(msg){
					if (msg != "") {
						$(".claroMessageBox:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
					}
					else {
							if (ajaxVal == 1) {
								$("#"+period+" .UDADOCshowHide").hide('fast');
								if ($("#"+period+" ul:visible").size() == 1)
								{
									$("#"+period+" .UDADOCshowHide").trigger('click');
								}
							}
							else {
								$("#"+period+" .UDADOCshowHide").show('fast');
								$("#"+period+" .UDADOCshowHide").trigger('click');
							}
					}
				},
				error: function(){
					return false;
				}
			});
		}
	);
	
	/******************************************
	 * Select / deselect classes
	 *****************************************/
	$("#UDADOCperiods input[@type=checkbox]").click(
		function(){
			if($(this).attr('checked') == true)
			var ajaxVal = 1;
			else
			var ajaxVal = 0;
			
			period = $(this).parents("ul").parents("li").attr("id");
			
			elem = $(this);
			
			$.ajax({
				data: "cmd=editVisClass&period="+period+"&class="+$(this).val()+"&visible="+ajaxVal,
				success: function(msg){
					if (msg != "") {
						$(".claroMessageBox:first").after('<table class="claroMessageBox"><tr><td>' + msg + '</td></tr></table>');
						elem.attr("checked",false);
					}
					else {
						elem.fadeOut();
						elem.fadeIn();
					}
				},
				error: function(){
					return false;
				}
			});
		}
	);
}


function initDateAndTimePicker()
{
		$("#startDate,#endDate").datepicker({
			dateFormat: "dd/mm/yy",
			showStatus: false,
			beforeShow: customRange
		});
		
		
		function customRange(input){
			return {
				minDate: (input.id == "endDate" ? $("#startDate").datepicker("getDate") : 0),
				maxDate: (input.id == "startDate" ? $("#endDate").datepicker("getDate") : 365)
			}
		};
}

function UDADOCget_lang(txt)
{
	if (UDADOClang[txt])
	return UDADOClang[txt];
	else
	return txt;
}
